package com.sjzd.server.controller;


import com.sjzd.server.pojo.Position;
import com.sjzd.server.pojo.RespBean;
import com.sjzd.server.service.IPositionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sjzd
 * @since 2021-02-21
 */
@Api("职位控制器")
@RestController
@RequestMapping("/sys/basic/pos")
public class PositionController {

    @Autowired
    private IPositionService positionService;

    @ApiOperation("获取所有职位信息")
    @GetMapping("/")
    public List<Position> getAllPosition() {
        return positionService.list();
    }

    @ApiOperation("添加职位")
    @PostMapping("/")
    public RespBean savePosition(@RequestBody Position pos) {
        if (null != pos) {
            pos.setCreateDate(LocalDateTime.now());
            pos.setEnabled(true);
            boolean success = positionService.save(pos);
            if (success) {
                return RespBean.success("添加成功！");
            }
        }

        return RespBean.error("添加失败！");
    }

    @ApiOperation("更改职位信息")
    @PutMapping("/")
    public RespBean updatePosition(@RequestBody Position pos) {
        if (null != pos) {
            boolean success = positionService.updateById(pos);
            if (success) {
                return RespBean.success("更改成功！");
            }
        }
        return RespBean.error("更改失败！");
    }

    @ApiOperation("删除职位信息")
    @DeleteMapping("/{id}")
    public RespBean deletePosition(@PathVariable Integer id) {
        boolean success = positionService.removeById(id);
        if (success) {
            return RespBean.success("删除成功！");
        }
        return RespBean.error("删除失败！");
    }

    @ApiOperation("批量删除职位信息")
    @DeleteMapping("/")
    public RespBean batchDeletePosition(Integer ids) {
        boolean success = positionService.removeByIds(Arrays.asList(ids));
        if (success) {
            return RespBean.success("批量删除成功！");
        }
        return RespBean.error("批量删除失败！");
    }
}
